<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Admin\Renderable\TagRender;
use App\Models\Posts\WxTag;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formPcTrait{
    public function index_pc(Content $content)
    {
        return $content->title('PC配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_pc()->render()}</div>
HTML
            );
    }


    protected function form_pc(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->tab('基础', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">tips：301跳转会附带一个url参数from，其值为真实的请求路径，而且301跳转后，浏览器会存在缓存</div>';
            }, '');
            $form->radio('pc_mdoe', 'PC端处理')->options([
                0 => '不处理',
                1 => '301跳转',
                2 => 'html页面',
            ])->default(Settings::get('pc_mdoe', 0));

            $form->listbox('pc_mdoe_page_range', '处理页面范围')->options([1=>'/', 2=>'/hot', 3=>'/tag/id', 4=>'/design/id', 5=>'/contact',
                6=>'/circles/id', 7=>'/circle/id', 8=>'/clause/id', 9=>'/login',
                10=>'/search/id/keyword', 11 => '/single/id', 12 => '/tool/id', 13=>'/404'])->customFormat(function ($value){
                return Settings::get('pc_mdoe_page_range', [1,2,3,4,5,6,7,8,9,10,11,12,13]);
            })->help('右边代表选中');

            $form->url('pc_mdoe_301_url', '301跳转地址')->placeholder('https://www.minisns.cn')->default(Settings::get('pc_mdoe_301_url', ''));
            $form->htmlEditor('pc_mdoe_html_code', 'html页面代码')->default(Settings::get('pc_mdoe_html_code', ''));
        });

        $form->tab('外观', function (Form $form) {
            $form->color('--color-primary', '--color-primary')->default(Settings::get('--color-primary', '#fc3a72'));
            $form->color('--color-secondary', '--color-secondary')->default(Settings::get('--color-secondary', '#2d3d8b'));
            $form->color('--primary-border-color', '--primary-border-color')->default(Settings::get('--primary-border-color', '#9b9b9b'));
            $form->color('--secondary-border-color', '--secondary-border-color')->default(Settings::get('--secondary-border-color', '#f0f8ff'));
            $form->color('--color-success', '--color-success')->default(Settings::get('--color-success', '#09815C'));
            $form->color('--color-danger', '--color-danger')->default(Settings::get('--color-danger', '#e3363e'));
            $form->color('--color-warning', '--color-warning')->default(Settings::get('--color-warning', '#e38836'));
            $form->color('--color-info', '--color-info')->default(Settings::get('--color-info', '#4da7d4'));
            $form->color('--color-light', '--color-light')->default(Settings::get('--color-light', '#f8f9f9'));
            $form->color('--color-grey', '--color-grey')->default(Settings::get('--color-grey', '#f7f8f9'));
            $form->color('--color-dark', '--color-dark')->default(Settings::get('--color-dark', '#000c2d'));
            $form->color('--color-light-dark', '--color-light-dark')->default(Settings::get('--color-light-dark', '#333333'));
            $form->color('--color-muted', '--color-muted')->default(Settings::get('--color-muted', '#687385'));
            $form->color('--color-white', '--color-white')->default(Settings::get('--color-white', '#FFFFFF'));
            $form->color('--mutted-border-color', '--mutted-border-color')->default(Settings::get('--mutted-border-color', '#eaecee'));
        });
        $form->tab('头部', function (Form $form) {
            $form->html(function () {
                return '<a href="'.admin_url('settings/menu').'" style="display: block; padding-top: 10px; color: cornflowerblue;">全局设置->菜单->父级菜单[id为1]</a>';
            }, '导航菜单');
        });
        $form->tab('页脚', function (Form $form) {
            $form->html(function () {
                return '<a href="'.admin_url('settings/menu').'" style="display: block; padding-top: 10px; color: cornflowerblue;">全局设置->菜单->父级菜单[id为2]</a>';
            }, '认证&合作');
            $form->html(function () {
                return '<a href="'.admin_url('settings/menu').'" style="display: block; padding-top: 10px; color: cornflowerblue;">全局设置->菜单->父级菜单[id为3]</a>';
            }, '赞助商&技术支持');
        });
        $form->tab('右侧栏', function (Form $form) {

        });
        $form->tab('首页', function (Form $form) {
            $form->text('pc_index_effect_character', '动效字')->help('就是首页那一行动效字，用|号隔开')->default(Settings::get('pc_index_effect_character', '我想真正的幸福只有一种，|就是自己拥有爱的能力，|珍重爱的人或它。|既不攀比，也很努力，|一觉醒来，面对暂新的生活与爱的ta。'));
            $form->listbox('pc_index_qrcode_list', '二维码展示')->options([
                'app' => 'App',
                'mp' => '公众号',
                'mini' => '小程序',
                'douyin' => '抖音'
            ])->customFormat(function ($value){
                return Settings::get('pc_index_qrcode_list', ['app', 'mini']);
            })->help('右边代表选中');
            $form->multipleSelectTable('app_hot_tags', '热门话题')
                ->title('话题库')
                ->max(10)
                ->from(TagRender::make())
                ->model(WxTag::class, 'id', 'tags_name', 'selectTable:formPcTrait:app_hot_tags')
                ->help('<a href="'.admin_url('tags').'" target="_blank">话题列表页</a>：如果为空，则根据使用次数排序。<a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_hot_tags', ''));
            $form->image('pc_index_right_banner', 'banner图')->help('建议和默认图片大小。 <a href="https://img.mini.minisns.cn/icons/pc_login_bg.png" download="pc_index_right_banner.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->autoUpload()->uniqueName()->default(Settings::get('pc_login_bg', 'https://img.mini.minisns.cn/icons/pc_login_bg.png'));
            $form->text('pc_index_contributors', '社区贡献者')->help('填写用户id，英文逗号隔开')->default(Settings::get('pc_index_contributors', '10000,10001,11580,11581,11583'));
        });
        $form->tab('登录', function (Form $form) {
            $form->image('pc_login_bg', '登录背景图')->help('建议和默认图片大小。 <a href="https://img.mini.minisns.cn/icons/pc_login_bg.png" download="app_login_bg.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->autoUpload()->default(Settings::get('pc_login_bg', 'https://img.mini.minisns.cn/icons/pc_login_bg.png'));
        });
        $form->tab('搜索', function (Form $form) {
            $form->text('pc_search_input_placeholder', 'placeholder')->help('搜索框提示语')->default(Settings::get('pc_search_input_placeholder', '在这里写下你想寻找的'));
            $form->text('pc_search_hot_keywords', '热门关键词')->help('英文逗号隔开，这里为空时，则调用搜索记录设置为热门的搜索')->default(Settings::get('pc_search_hot_keywords', '第一次养宠物,拉肚子,挑食'));
        });
        $form->action('settings/action/pc');
        return $form;
    }

    public function pc(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('pc_mdoe', $request->input('pc_mdoe'));
        Settings::set('pc_mdoe_page_range', $request->input('pc_mdoe_page_range'));
        Settings::set('pc_mdoe_301_url', $request->input('pc_mdoe_301_url'));
        Settings::set('pc_mdoe_html_code', $request->input('pc_mdoe_html_code'));

        Settings::set('--color-primary', $request->input('--color-primary'));
        Settings::set('--color-secondary', $request->input('--color-secondary'));
        Settings::set('--primary-border-color', $request->input('--primary-border-color'));
        Settings::set('--secondary-border-color', $request->input('--secondary-border-color'));
        Settings::set('--color-success', $request->input('--color-success'));
        Settings::set('--color-danger', $request->input('--color-danger'));
        Settings::set('--color-warning', $request->input('--color-warning'));
        Settings::set('--color-info', $request->input('--color-info'));
        Settings::set('--color-light', $request->input('--color-light'));
        Settings::set('--color-grey', $request->input('--color-grey'));
        Settings::set('--color-dark', $request->input('--color-dark'));
        Settings::set('--color-muted', $request->input('--color-muted'));
        Settings::set('--color-white', $request->input('--color-white'));
        Settings::set('--mutted-border-color', $request->input('--mutted-border-color'));
        Settings::set('app_hot_tags', $request->input('app_hot_tags'), true);

        $admin_color = [
            '--color-primary' => $request->input('--color-primary'),
            '--color-secondary' => $request->input('--color-secondary'),
            '--primary-border-color' => $request->input('--primary-border-color'),
            '--secondary-border-color' => $request->input('--secondary-border-color'),
            '--color-success' => $request->input('--color-success'),
            '--color-danger' => $request->input('--color-danger'),
            '--color-warning' => $request->input('--color-warning'),
            '--color-info' => $request->input('--color-info'),
            '--color-light' => $request->input('--color-light'),
            '--color-grey' => $request->input('--color-grey'),
            '--color-dark' => $request->input('--color-dark'),
            '--color-muted' => $request->input('--color-muted'),
            '--color-white' =>  $request->input('--color-white'),
            '--mutted-border-color' => $request->input('--mutted-border-color'),
        ];
        Cache::forget('admin_color');
        Cache::put('admin_color', json_encode($admin_color));
        Settings::set('pc_index_qrcode_list', $request->input('pc_index_qrcode_list'), true);

        Settings::set('pc_index_effect_character', $request->input('pc_index_effect_character'), true);
        Settings::set('pc_index_contributors', $request->input('pc_index_contributors'), true);

        Settings::set('pc_index_right_banner', $request->input('pc_index_right_banner'), true);

        Settings::set('pc_search_input_placeholder', $request->input('pc_search_input_placeholder'), true);
        Settings::set('pc_search_hot_keywords', $request->input('pc_search_hot_keywords'), true);

        Settings::set('pc_login_bg', $request->input('pc_login_bg'), true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
